package com.pn.service.impl;

import com.pn.entity.Purchase;
import com.pn.entity.Result;
import com.pn.page.Page;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.pn.mapper.PurchaseMapper;
import com.pn.service.PurchaseService;

import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class PurchaseServiceImpl implements PurchaseService{

    @Resource
    private PurchaseMapper purchaseMapper;

    @Override
    public int addPurchase(Purchase purchase) {
        int result = purchaseMapper.insertPurchase(purchase);
        return result;
    }

    @Override
    public Page findPurchaseByPage(Purchase purchase, Page page) {
        page.setTotalNum(purchaseMapper.countPurchase(purchase));
        List<Purchase> allPurchase = purchaseMapper.findAllPurchase(purchase, page);
        for (Purchase p:allPurchase
             ) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
        page.setResultList(allPurchase);
        return page;
    }

    @Override
    public Result removePurchaseByBid(Integer bid) {
        int result = purchaseMapper.deletePurchaseByBid(bid);
        if (result > 0){
            return Result.ok("采购单记录删除成功！");
        }else {
            return Result.err(Result.CODE_ERR_BUSINESS,"采购单记录删除失败！");
        }
    }

    @Override
    public Result modifyPurchaseNumByBid(Purchase purchase) {
        int result = purchaseMapper.updatePurchaseNumByBid(purchase);
        if (result > 0){
            return Result.ok("采购单数据更新成功！");
        }else {
            return Result.err(Result.CODE_ERR_BUSINESS,"采购单数据更新失败！");
        }
    }
}
